package com.example.sofa_module.picturesofa;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.bw.adapter_sdk.quick.BaseQuickAdapter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.network_sdk.videoentity.VideoEntity;
import com.bw.network_sdk.videoentity.VideoValuesEntity;
import com.bw.sofa_module.R;
import com.bw.sofa_module.databinding.FragmentPictureBinding;
import com.bw.sofa_module.entity.diss.DissEntity;
import com.bw.sofa_module.entity.likestate.LikeStateEntity;

import java.util.List;

public class PictureFragment extends BaseFragment<FragmentPictureBinding,PictureViewModel> implements BaseQuickAdapter.OnItemClickChildListener {
    private  RecyclerView.RecycledViewPool recycledViewPool ;

    public PictureFragment(RecyclerView.RecycledViewPool recycledViewPool) {
        this.recycledViewPool = recycledViewPool;
    }

    private PictureAdapter pictureAdapter;
    private int position = -1;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentPictureBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setRecycleChildrenOnDetach(true);
        v.picturesofaRecycler.setLayoutManager(linearLayoutManager);
        pictureAdapter = new PictureAdapter();
        pictureAdapter.setOnItemClickChildListener(this);
        if (recycledViewPool!=null){
            v.picturesofaRecycler.setRecycledViewPool(recycledViewPool);
        }
        v.picturesofaRecycler.setAdapter(pictureAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                VideoEntity videoEntity = (VideoEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                pictureAdapter.setNewInstance(videoEntity.data.data);
                break;
            case LIKES:
                LikeStateEntity likeEntity = (LikeStateEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                pictureAdapter.getData().get(position).ugc.hasLiked = likeEntity.data.data.hasLiked;
                if (likeEntity.data.data.hasLiked)
                    pictureAdapter.getData().get(position).ugc.likeCount = vm.count;
                pictureAdapter.notifyItemChanged(position);
                break;
            case DISS:
                DissEntity dissEntity = (DissEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                pictureAdapter.getData().get(position).ugc.hasdiss = dissEntity.data.data.hasLiked;
                pictureAdapter.notifyItemChanged(position);
                break;
        }
    }
    @Override
    public void onItemClickChild(RecyclerView.Adapter adapter, int id, int position) {
        PictureAdapter pictureAdapters = (PictureAdapter) adapter;
        List<VideoValuesEntity> data = pictureAdapters.getData();
        if (id == R.id.item_picture_likes_img) {
            this.position = position;
            vm.onLike(data.get(position).itemId);
        } else if (id == R.id.item_picture_unlikes_img) {
            this.position = position;
            vm.onDiss(data.get(position).itemId);
        } else if (id == R.id.item_picture_comment_img) {
        } else if (id == R.id.item_picture_share_img) {
        }
    }
}